package com.zhima.controller.plain;

import com.zhima.annotation.MustLogin;
import com.zhima.dto.QueryQuestionCondition;
import com.zhima.dto.ZhimaOjPageResult;
import com.zhima.dto.ZhimaOjResult;
import com.zhima.service.OjQuestionPublishService;
import com.zhima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.zhima.dto.ZhimaOjResult.success;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhima
 * @since 2023-08-25
 */
@RestController
@RequestMapping("/publish")
public class OjQuestionPublishController {
    @Autowired
    private OjQuestionPublishService ojQuestionPublishService;

    /**
     * 分页查询登录用户提交的发布记录
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryQuestionPublish(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return ZhimaOjPageResult.success(ojQuestionPublishService.pageQueryQuestionPublish(UserHolder.get().getId(), pageNum, pageSize));
    }

    /**
     * 返回指定题目详细信息
     *
     * @param questionId
     * @return
     */
    @GetMapping("/{questionId}")
    @MustLogin
    public ZhimaOjResult queryQuestionDetailById(@PathVariable("questionId") Long questionId) {
        return success(ojQuestionPublishService.queryQuestionDetailById(UserHolder.get().getId(), questionId));
    }

    /**
     * 用户提交发布题目
     *
     * @param questionId
     * @return
     */
    @PostMapping("/{questionId}")
    public ZhimaOjResult submitPublishQuestion(@PathVariable("questionId") Long questionId) {
        return success(ojQuestionPublishService.submitPublishQuestion(UserHolder.get().getId(), questionId));
    }

    /**
     * 多条件查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/s/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryQuestionPublishByConditions(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize, @RequestBody QueryQuestionCondition queryQuestionCondition) {
        return ZhimaOjPageResult.success(ojQuestionPublishService.pageQueryQuestionPublishByConditions(pageNum, pageSize, UserHolder.get() == null ? null : UserHolder.get().getId(), queryQuestionCondition));
    }

    /**
     * 随机返回一道题目的若干个AC用户
     *
     * @param questionId
     * @return
     */
    @GetMapping("/ac/{questionId}")
    public ZhimaOjResult questionRandomAcUser(@PathVariable("questionId") Long questionId) {
        return success(ojQuestionPublishService.questionRandomAcUser(questionId));
    }

    /**
     * 取消发布指定题目
     *
     * @param questionId
     * @return
     */
    @PostMapping("/cancel/{questionId}")
    public ZhimaOjResult cancelSubmitQuestionPublish(@PathVariable("questionId") Long questionId) {
        return success(ojQuestionPublishService.cancelSubmitQuestionPublish(questionId));
    }
}
